package com.element5th.shop.servlet.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.entity.User;

/**
 * Servlet implementation class Recharge
 */
@WebServlet("/Recharge")
public class Recharge extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Recharge() {
        super();
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//拿到页面输入的金额
		String money = request.getParameter("money");		
		System.out.println(money);
		//字符转换
		Double nowM =Double.parseDouble(money);
		//通过session拿到dto
		HttpSession session = request.getSession();
		BaseDto userdto = new BaseDto();
		userdto =  (BaseDto) session.getAttribute("userDto");
		String username = userdto.getUser().getUserName();
		Double User_Balance=userdto.getUser().getUserBalance();
		request.setAttribute("username", username);
		request.setAttribute("User_Balance", User_Balance);
		//通过session拿到对象的id 
		int id =userdto.getUser().getUserId();
		User u =RecharImp.select(userdto,id);
		Double sum = u.getUserBalance()+nowM;
		//设置充值后的余额 调用update方法
        userdto.getUser().setUserBalance(sum);
        RecharImp.update(userdto);
        request.setAttribute("User_Balance", userdto.getUser().getUserBalance());
        request.setAttribute("username", userdto.getUser().getUserName());
        request.getServletContext().getRequestDispatcher("/Style.jsp").forward(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doGet(request, response);
	}

}
